<template>
  <div class="product-list">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.productName"
            placeholder="请输入商品名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="供应商">
          <el-input
            v-model="searchForm.supplierName"
            placeholder="请输入供应商"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="operation-section">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
        新增商品
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="productList"
      border
      stripe
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="productName" label="商品名称" min-width="150" />
      <el-table-column prop="imageUrl" label="商品图片" width="100">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.imageUrl"
            :preview-src-list="[scope.row.imageUrl]"
            fit="cover"
            style="width: 50px; height: 50px"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
            <div slot="placeholder" class="image-slot">
              <i class="el-icon-loading"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="supplierName" label="供应商" min-width="120" />
      <el-table-column prop="price" label="价格" width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination-section">
      <el-pagination
        :current-page.sync="pageNum"
        :page-size="pageSize"
        :total="total"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getProductList, deleteProduct } from '@/api/products'

export default {
  name: 'ProductList',
  data() {
    return {
      searchForm: {
        productName: '',
        supplierName: ''
      },
      productList: [],
      pageNum: 1,
      pageSize: 5,
      total: 0,
      loading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      try {
        const params = {
          pageNum: this.pageNum,
          productName: this.searchForm.productName,
          supplierName: this.searchForm.supplierName
        }
        const res = await getProductList(params)
        if (res.code === 200) {
          this.productList = res.data.list
          this.total = res.data.total
        }
      } catch (error) {
        console.error('获取商品列表失败:', error)
        this.$message.error('获取商品列表失败')
      } finally {
        this.loading = false
      }
    },
    handleSearch() {
      this.pageNum = 1
      this.getList()
    },
    resetSearch() {
      this.searchForm = {
        productName: '',
        supplierName: ''
      }
      this.handleSearch()
    },
    handleAdd() {
      this.$router.push('/products/add')
    },
    handleEdit(row) {
      this.$router.push(`/products/edit/${row.productId}`)
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该商品吗？', '提示', {
          type: 'warning'
        })
        const res = await deleteProduct(row.productId)
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.getList()
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除商品失败:', error)
          this.$message.error('删除失败')
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.product-list {
  padding: 20px;

  .search-section {
    background: #fff;
    padding: 18px 20px;
    border-radius: 4px;
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  }

  .operation-section {
    margin-bottom: 16px;
  }

  .el-table {
    margin-bottom: 16px;
  }

  .pagination-section {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
    padding: 10px 20px;
    background: #fff;
    border-radius: 4px;
  }

  .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #909399;
    font-size: 20px;
  }
}
</style> 